package com.onenine.chat.message;


import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.onenine.app.BuildConfig;
import com.onenine.app.R;
import com.onenine.base.view.NiceImageView;
import com.onenine.chat.commons.IMessage;
import com.onenine.chat.model.LUser;
import com.onenine.chat.rongcloud.UserCacheMgr;
import com.onenine.chat.shape.RoundTextView;
import com.onenine.common.util.TimeUtils;

import io.rong.imlib.model.Conversation;


public class PhotoViewHolder<MESSAGE extends IMessage> extends BaseMessageViewHolder<MESSAGE>
        implements MsgListAdapter.DefaultMessageViewHolder {

    private boolean mIsSender;
    private TextView mNameTv;
    private RoundTextView mDateTv;
    private ImageView mPhotoIv;
    private NiceImageView mAvatarIv;
    private ProgressBar mSendingPb;
    private ImageButton mResendIb;

    public PhotoViewHolder(View itemView, boolean isSender) {
        super(itemView);
        this.mIsSender = isSender;
        mDateTv = itemView.findViewById(R.id.aurora_tv_msgitem_date);
        mNameTv = itemView.findViewById(R.id.aurora_tv_msgitem_display_name);
        mPhotoIv = itemView.findViewById(R.id.aurora_iv_msgitem_photo);
        mAvatarIv = itemView.findViewById(R.id.aurora_iv_msgitem_avatar);
        if (mIsSender) {
            mSendingPb = itemView.findViewById(R.id.aurora_pb_msgitem_sending);
            mResendIb = itemView.findViewById(R.id.aurora_ib_msgitem_resend);
        }
    }

    @Override
    public void onBind(final MESSAGE message, final int position) {
        mDateTv.setText(TimeUtils.convertWXTimeStr(message.getDatetime()));
        setDatetimeVisibility(mDateTv, position);
        if (message.getConversationType() == Conversation.ConversationType.GROUP) {
            mNameTv.setVisibility(View.VISIBLE);
        } else {
            mNameTv.setVisibility(View.GONE);
        }
        UserCacheMgr.getInstance().getUserAsync(message.getSentUserId(), new UserCacheMgr.OnUserCallback() {
            @Override
            public void callback(LUser user) {
                mNameTv.setText(user.getNickName());
                mImageLoader.loadAvatarImage(mAvatarIv, user.getPhoto());
                mAvatarIv.setTag(R.id.data_tag, user.getPhoto());
            }
        });
        if (mScroll) {
            mPhotoIv.setImageResource(R.drawable.ic_post_img);
        } else {
            mImageLoader.loadImage(mPhotoIv, message.getText(), message.getExtra());
        }
        mAvatarIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mAvatarClickListener != null) {
                    mAvatarClickListener.onAvatarClick(mAvatarIv, message);
                }
            }
        });

        mPhotoIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mMsgClickListener != null) {
                    mMsgClickListener.onMessageClick(view, message);
                }
            }
        });

        mPhotoIv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (mMsgLongClickListener != null) {
                    mMsgLongClickListener.onMessageLongClick(view, message);
                } else {
                    if (BuildConfig.DEBUG) {
                        Log.w("MsgListAdapter", "Didn't set long click listener! Drop event.");
                    }
                }
                return true;
            }
        });
        if (mIsSender) {
            switch (message.getMsgSentStatus()) {
                case SENDING:
                    mSendingPb.setVisibility(View.VISIBLE);
                    mResendIb.setVisibility(View.GONE);
                    break;
                case FAILED:
                    mResendIb.setVisibility(View.VISIBLE);
                    mSendingPb.setVisibility(View.GONE);
                    mResendIb.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mMsgStatusViewClickListener != null) {
                                mMsgStatusViewClickListener.onStatusViewClick(message);
                            }
                        }
                    });
                    break;
                case SENT:
                    mSendingPb.setVisibility(View.GONE);
                    mResendIb.setVisibility(View.GONE);
                    break;
            }
        }
    }

    @Override
    public void applyStyle(MessageListStyle style) {
        mDateTv.setTextSize(style.getDateTextSize());
        mDateTv.setTextColor(style.getDateTextColor());
        mDateTv.setPadding(style.getDatePaddingLeft(), style.getDatePaddingTop(),
                style.getDatePaddingRight(), style.getDatePaddingBottom());
        mDateTv.setBgCornerRadius(style.getDateBgCornerRadius());
        mDateTv.setBgColor(style.getDateBgColor());
        if (mIsSender) {
            mPhotoIv.setBackground(style.getSendPhotoMsgBg());
            if (style.getSendingProgressDrawable() != null) {
                mSendingPb.setProgressDrawable(style.getSendingProgressDrawable());
            }
            if (style.getSendingIndeterminateDrawable() != null) {
                mSendingPb.setIndeterminateDrawable(style.getSendingIndeterminateDrawable());
            }
        } else {
            mPhotoIv.setBackground(style.getReceivePhotoMsgBg());
        }
        android.view.ViewGroup.LayoutParams layoutParams = mAvatarIv.getLayoutParams();
        layoutParams.width = style.getAvatarWidth();
        layoutParams.height = style.getAvatarHeight();
        mAvatarIv.setLayoutParams(layoutParams);
    }

}
